<?php
namespace App\Models;

use Cache;

class Depart extends BaseModel
{
    const KEY = 'cache_all';

    protected $table = "depart";


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }


    // 获取我的优惠券列表
    public function getDepartList()
    {
        $rows = $this->getCachAllList();
        return $rows;
    }

    /**
     * 重新生成缓存
     */
    public function generateCache()
    {
        Cache::forget(self::KEY);
        $where = [];
        $order['sort'] = 'asc';
        $list = $this->getList($where, '*', $order);
        if ($list) {
            $list = $list->toArray();
        }
        $arr = $list;
        Cache::forever(self::KEY, $arr);
    }

    /**
     *  获取单条数据
     */
    public function getOneData($request)
    {
        $where['id'] = $request['id'];
        $res = $this->getOne($where)->toArray();
        return ['state' => 1, 'msg' => 'ok', 'data' => $res];
    }

    /**
     * 获取缓存
     */
    public function getCachAllList()
    {
        if (Cache::has(self::KEY)) {
            return Cache::get(self::KEY);
        } else {
            $where = [];
            $order['sort'] = 'asc';
            $list = $this->getList($where, '*', $order);
            if ($list) {
                $list = $list->toArray();
            }
            $arr = $list;
            Cache::forever(self::KEY, $arr);
            return $arr;
        }
    }

    /**
     * 编辑科室
     */
    public function editDepart($request)
    {
        $where['name'] = $request['name'];
        $where['id'] = ['<>',$request['id']];
        $res = $this->getOne($where);
        if ($res) {
            return '该科室名称已存在';
        }
        $data['name'] = $request['name'];
        $data['sort'] = $request['sort'];
        $data['reffer_id'] = $request['reffer_id'];
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $res = $this->up(['id'=>$request['id']],$data);
        if (!$res) {
            return '编辑科室失败';
        }
        $this->generateCache();
        return '编辑科室成功';
    }

    /**
     * 添加科室
     * @param $request
     */
    public function AddDepart($request)
    {
        $res = $this->getOne(['name' => $request['name']]);
        if ($res) {
            return '该科室名称已存在';
        }
        $data['name'] = $request['name'];
        $data['sort'] = $request['sort'];
        $data['reffer_id'] = $request['reffer_id'];
        $data['created_at'] = date("Y-m-d", time());
        $res = $this->add($data);
        if (!$res) {
            return '添加科室失败';
        }
        $this->generateCache();
        return '添加科室成功';
    }

    public function getDepartInfoById($id)
    {
        $res = $this->getOne(['id' => $id]);
        return $res;
    }

    public function delDepart($request)
    {
        $res = $this->del(['id'=>$request['id']]);
        $data['status'] = $res ? 1 : 0;
        $data['msg'] = '删除失败';
        if ($res) {
            $data['msg'] = '删除成功';
            $this->generateCache();
        }
        return $data;
    }

    public function his_reg_doctors($departId)
    {
        $depart = $this->getDepartInfoById($departId);
        $data['bck01'] = $depart['reffer_id'];
        $data['isduty'] = 0;
        return $data;
    }
}